package com.tuanzi.loan.business.service.family.impl;

import com.base.common.log.LogService;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.family.LoanWholeSaleComment;
import com.tuanzi.loan.business.repository.family.LoanWholeSaleCommentRepository;
import com.tuanzi.loan.business.service.family.LoanWholeSaleCommentService;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.LoanWholeSaleCommentSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanWholeSaleCommentSearchResponse;
import com.tuanzi.loan.vo.view.family.CommRegionNewInfo;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class LoanWholeSaleCommentServiceImpl extends BaseAuditServiceImpl<LoanWholeSaleComment, Long> implements LoanWholeSaleCommentService {
    private final String DATEFORMAT = "yyyy-MM-dd";
    private final Integer UPPERSTATUS =  1;
    private final Integer DOWNSTATUS = 0;
    @Autowired
    DictionaryService service;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    private LoanWholeSaleCommentRepository loanWholeSaleCommentRepository;
    
    @Override
    public Page<LoanWholeSaleCommentSearchResponse> list(PageSearcher<LoanWholeSaleCommentSearchRequest> pageSearcher) {
        return loanWholeSaleCommentRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
    }

    @Override
    public Integer saveLoanWholeSaleComment(LoanWholeSaleCommentSearchRequest request) {
        deleteRedis(request.getType());
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        LoanWholeSaleComment tt = new LoanWholeSaleComment();
        LoanWholeSaleComment loanWholeSaleComment = new LoanWholeSaleComment();
        if (request.getId()!=null){
            loanWholeSaleComment.setId(request.getId());
        }
        //编辑和创建新增
        loanWholeSaleComment.setAmount(request.getAmount());
        loanWholeSaleComment.setApprovenum(request.getApprovenum());
        loanWholeSaleComment.setAvgprice(request.getAvgprice());
        loanWholeSaleComment.setCooperate(request.getCooperate());
        try {
            loanWholeSaleComment.setCreatedate(new SimpleDateFormat(DATEFORMAT).parse(DateUtils.getCurrentTime()));
        } catch (ParseException e) {
            LogService.error("日期转换异常",e);
        }
        loanWholeSaleComment.setOwnername(request.getOwnername());
        loanWholeSaleComment.setSort(request.getSort());
        loanWholeSaleComment.setNumber(request.getNumber());
        loanWholeSaleComment.setUppertime(sdf.format(request.getUppertime()));
        loanWholeSaleComment.setDowntime(sdf.format(request.getDowntime()));
        loanWholeSaleComment.setStatus(request.getStatus());
        loanWholeSaleComment.setType(request.getType());
        loanWholeSaleComment.setOnlinedate(request.getOnlinedate().toString().replace("[","").replace("]",""));
        loanWholeSaleComment.setCreator(request.getEditor());
        loanWholeSaleComment.setUpdator(request.getEditor());

        loanWholeSaleComment.setHas_car(translateFalg(request.isHas_car()));
        loanWholeSaleComment.setHas_estate(translateFalg(request.isHas_estate()));
        loanWholeSaleComment.setHas_weilidai(translateFalg(request.isHas_weilidai()));
        loanWholeSaleComment.setSocial_insurance(translateFalg(request.isSocial_insurance()));
        loanWholeSaleComment.setCredit(translateFalg(request.isCredit()));
        loanWholeSaleComment.setLife_insurance(translateFalg(request.isLife_insurance()));
        loanWholeSaleComment.setHouse_fund(translateFalg(request.isHouse_fund()));
        loanWholeSaleComment.setSocial_insuranceduration(request.getSocial_insuranceduration());
        loanWholeSaleComment.setHouse_fundduration(request.getHouse_fundduration());
        loanWholeSaleComment.setComm_regoin(request.getCommRegionNewlist().toString().replace("[","").replace("]",""));
        loanWholeSaleComment.setDaikuan_amount(request.getDaikuan_amount());
        loanWholeSaleComment.setIncome(request.getIncome());
        loanWholeSaleComment.setMaxage(request.getMaxage());
        loanWholeSaleComment.setMinage(request.getMinage());
        loanWholeSaleComment.setWaitui(translateFalg(request.isWaitui()));
        loanWholeSaleComment.setZhannei(translateFalg(request.isZhannei()));

        tt =loanWholeSaleCommentRepository.saveAndFlush(loanWholeSaleComment);
        if (tt!=null){
            deleteRedis(loanWholeSaleComment.getType());
            return 1;
        }
        return 0;
    }

    @Override
    public Integer updateStatus(Long id) {
        LoanWholeSaleComment loanWholeSaleComment = findOne(id);
        Integer status =  loanWholeSaleComment.getStatus();
        if (status == UPPERSTATUS){
            loanWholeSaleComment.setStatus(DOWNSTATUS);
            loanWholeSaleComment.setUpdator(CurrentUser.get().getName());
            if (saveOrUpdate(loanWholeSaleComment)!=null){
                deleteRedis(loanWholeSaleComment.getType());
                return 1;
            }
        }else if(status==DOWNSTATUS){
            loanWholeSaleComment.setStatus(UPPERSTATUS);
            loanWholeSaleComment.setUpdator(CurrentUser.get().getName());
            if (saveOrUpdate(loanWholeSaleComment)!=null){
                deleteRedis(loanWholeSaleComment.getType());
                return 1;
            }
        }
        return 0;
    }

    /**
     * 删除大额api缓存及投递管理缓存
     * @param type
     */
    private void deleteRedis(Integer type){
        stringRedisTemplate.delete(LoanRedisKey.LOAN_WHOLESALE_COMMENT_ALL);
        stringRedisTemplate.delete(LoanRedisKey.LOAN_LARGE_OUTER_NEW_DELIVERY_LIST);
        stringRedisTemplate.delete(LoanRedisKey.LARGE_ONLINE_PRODUCT_LIST);
    }

    private String translateFalg(boolean falg){
        return falg?"是":"否";
    }
    @Override
    public Integer updateLoanWholeSaleComment(LoanWholeSaleComment request) {
        if (loanWholeSaleCommentRepository.updateLoanWholeSaleComment(request)>0){
            deleteRedis(request.getType());
            return 1;
        }
        return 0;
    }

    @Override
    public List<CommRegionNewInfo> findAllRegionInfo() {
        return loanWholeSaleCommentRepository.findAllRegionInfo();
    }

    @Override
    public Integer saveLoanWholeSaleComment(LoanWholeSaleComment request) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        LoanWholeSaleComment tt = new LoanWholeSaleComment();
        tt =loanWholeSaleCommentRepository.saveAndFlush(request);
        if (tt!=null){
            deleteRedis(request.getType());
            return 1;
        }
        return 0;
    }

    @Override
    public LoanWholeSaleComment findLoanWholeSaleCommentById(Long id) {
        return loanWholeSaleCommentRepository.findById(id);
    }

    @Override
    public void OnDelete(LoanWholeSaleCommentSearchResponse response) {
        loanWholeSaleCommentRepository.delete(NumberUtils.toLong(String.valueOf(response.getId())));
        deleteRedis(response.getType());
    }

    @Override
    public List<LoanWholeSaleComment> findAllByStatus(Integer status) {
        return loanWholeSaleCommentRepository.findAllByStatus(status);
    }

    @Override
    protected JPAAuditRepository<LoanWholeSaleComment, Long> getRepository() {
        return loanWholeSaleCommentRepository;
    }
}
